package com.sistema.venda.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {
	
	private static final SessionFactory sessionFactory = buildSessionFatory();
	private static ServiceRegistry serviceRegistry;

	private static SessionFactory buildSessionFatory() {
		try {
			Configuration cfg = new Configuration();
			cfg.configure();				
			serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
			return cfg.buildSessionFactory(serviceRegistry);	
		} catch (Throwable e) {
			System.err.println("Erro ao conectar o banco " + e);
			throw new ExceptionInInitializerError();
		}
	}

	public static SessionFactory getSessionfactory() {
		return sessionFactory;
	}
	
}
